package com.bstek.demo.urule.client;

import com.bstek.demo.urule.JsonUtils;
import com.bstek.demo.urule.client.model.SportData;
import com.bstek.demo.urule.client.model.SportMan;
import com.bstek.demo.urule.client.model.SportProject;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.bstek.urule.runtime.service.KnowledgeService;
import com.bstek.frame.model.ApiResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 体育项目：案例8的测试
 */
@RestController
@RequestMapping("urule")
public class SportController {
	
    /**
     * 体育比赛项目测试：长跑，短跑
     * @return
     * @throws IOException
     */
   	@RequestMapping("sport")
       public ApiResult<?> test4() throws IOException{
           KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
           KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("21526");
           KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
           
           SportMan sportMan=new SportMan("韩大大","男",30);
           //构造参赛项目数据集合
           List<SportProject> projectData=new ArrayList<>();
           //模拟长跑成绩
           List<SportData> longProjectData=new ArrayList<>();
           SportData sportData=new SportData("10000米",30);
           longProjectData.add(sportData);
           sportData=new SportData("20000米",60);
           longProjectData.add(sportData);
           sportData=new SportData("30000米",90);
           longProjectData.add(sportData);
           
           SportProject longProject=new SportProject();
           longProject.setFirstProject("长跑项目");
           longProject.setSportData(longProjectData);
           projectData.add(longProject);
           //模拟短跑成绩
           List<SportData> shortProjectData=new ArrayList<>();
           sportData=new SportData("100米",1);
           shortProjectData.add(sportData);
           sportData=new SportData("200米",3);
           shortProjectData.add(sportData);
           sportData=new SportData("400米",9);
           shortProjectData.add(sportData);
          
           SportProject shortProject=new SportProject();
           shortProject.setFirstProject("短跑项目");
           shortProject.setSportData(shortProjectData);
           projectData.add(shortProject);
           
           sportMan.setProjectData(projectData);
           
           session.insert(sportMan);
           RuleExecutionResponse res= session.fireRules();
           
           System.out.println("run complete:"+ JsonUtils.toJSON(res));
           /*
            * FIXME 计算最终成绩评定结果：
            * 一级项目有一个不及格，最终评定为【不合格】或者是 根据短跑总得分的范围评定
           */
           // 取短跑总成绩
           for(SportProject  project : sportMan.getProjectData()) {
        	   if(project.getFirstProject()=="短跑项目") {
        		   if(project.getFirstScore()>=250) {
        			   sportMan.setTotalGrade("优秀");
        		   }else  if(project.getFirstScore()>=200 && project.getFirstScore()<250) {
        			   sportMan.setTotalGrade("良好");
        		   }else  if(project.getFirstScore()>=150 && project.getFirstScore()<200) {
        			   sportMan.setTotalGrade("及格");
        		   }else  if(project.getFirstScore()<150) {
        			   sportMan.setTotalGrade("不及格");
        		   }
        		   break;
        	   }
           }
           
           Integer totalFailTimes=(Integer) session.getParameter("totalFailTimes");
           if(totalFailTimes>0) {
			   sportMan.setTotalGrade("不及格");
           }
           
           return ApiResult.success(sportMan);
       }
       

}
